package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.db.DBConn;
import dao.OutcomeMagDao;
import domain.LearnRes;
import domain.Outcome;

/**
 * 
 * Jun 5, 2009
 *
 * @author Fabian
 *
 */
public class OutcomeMagImpl implements OutcomeMagDao {

	/* (non-Javadoc)
	 * @see dao.OutcomeMagDao#addOutcome(vo.Outcome)
	 */
	
	public boolean addOutcome(Outcome o) {
		boolean result = false;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "insert into t_outcome values (null, ?, ?, ?, ?, ?, ?, '无', now(), ?, '无', '无')";
		//修改t_homework的sub_count
		String sql2 = "update t_homework set sub_count = sub_count + 1 where id = ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setInt(1, o.getHw_id());
			pstmt.setInt(2, o.getStu_id());
			pstmt.setString(3, o.getStu_name());
			pstmt.setString(4, o.getFormat());
			pstmt.setString(5, o.getSize());
			pstmt.setString(6, o.getPath());
			pstmt.setString(7, o.getIs_visto());
			int rsColum = pstmt.executeUpdate();		
			if (rsColum > 0) {
				pstmt = DBConn.getPstmt(conn, sql2);
				pstmt.setInt(1, o.getHw_id());
				int rsColum2 = pstmt.executeUpdate();
				if (rsColum2 > 0) {
					result = true;
					return result;
				}
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return result;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return result;
	}

	/* (non-Javadoc)
	 * @see dao.OutcomeMagDao#delOutcome(int)
	 */
	
	public boolean delOutcome(int id) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see dao.OutcomeMagDao#getPageOutcome(int, int, int)
	 */
	
	public List<Outcome> getPageOutcome(int start, int limit, int hw_id) {
		List<Outcome> list = new ArrayList<Outcome>();		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select * from t_outcome where hw_id = ? order by pub_time desc limit ?, ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setInt(1, hw_id);
			pstmt.setInt(2, start);
			pstmt.setInt(3, limit);
			rs = pstmt.executeQuery();			
			while (rs.next()) {
				Outcome o = new Outcome();
				o.setId(rs.getInt("id"));
				o.setStu_name(rs.getString("stu_name"));
				o.setFormat(rs.getString("format"));
				o.setSize(rs.getString("size"));
				o.setPath(rs.getString("path"));
				o.setVisto_path(rs.getString("visto_path"));
				o.setPub_time(rs.getTimestamp("pub_time"));
				o.setIs_visto(rs.getString("is_visto"));
				o.setScore(rs.getString("score"));
				o.setPostil(rs.getString("postil"));
				
				list.add(o);
			}
			return list;
		} catch(SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
	}

	/* (non-Javadoc)
	 * @see dao.OutcomeMagDao#getTotalCount(int)
	 */
	
	public int getTotalCount(int hw_id) {
		int totalCount = 0;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select count(*) from t_outcome where hw_id = ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setInt(1, hw_id);
			rs = pstmt.executeQuery();			
			if (rs.next()) {
				totalCount = rs.getInt(1);
				return totalCount;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return totalCount;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		return 0;
	}

	/* (non-Javadoc)
	 * @see dao.OutcomeMagDao#modifyOutcome(vo.Outcome)
	 */
	
	public boolean modifyOutcome(Outcome o) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see dao.OutcomeMagDao#getOutcome(int, int)
	 */
	
	public Outcome getOutcome(int hw_id, int stu_id) {
		Outcome o = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;		
		String sql = "select * from t_outcome where hw_id = ? and stu_id = ?";
		
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setInt(1, hw_id);
			pstmt.setInt(2, stu_id);
			rs = pstmt.executeQuery();			
			if (rs.next()) {
				o = new Outcome();
				o.setId(rs.getInt("id"));
				o.setFormat(rs.getString("format"));
				o.setSize(rs.getString("size"));
				o.setVisto_path(rs.getString("visto_path"));
				o.setPub_time(rs.getTimestamp("pub_time"));
				o.setIs_visto(rs.getString("is_visto"));
				o.setScore(rs.getString("score"));
				o.setPostil(rs.getString("postil"));
				return o;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		return o;
	}

	/* (non-Javadoc)
	 * @see dao.OutcomeMagDao#markOutcome(vo.Outcome)
	 */
	
	public boolean markOutcome(Outcome o) {
		boolean result = false;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "update t_outcome set score=?, postil=? where id = ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setString(1, o.getScore());
			pstmt.setString(2, o.getPostil());
			pstmt.setInt(3, o.getId());
			int rsColum = pstmt.executeUpdate();
			if (rsColum > 0) {
				result = true;
				return result;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return result;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return result;
	}

	/* (non-Javadoc)
	 * @see dao.OutcomeMagDao#addVistoOutcome(vo.Outcome)
	 */
	
	public boolean addVistoOutcome(Outcome o) {
		boolean result = false;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "update t_outcome set visto_path = ?, is_visto = ? where id = ?";
		//修改t_homework的correct_count
		String sql2 = "update t_homework set correct_count = correct_count + 1 where id = (select hw_id from t_outcome where id = ?)";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setString(1, o.getVisto_path());
			pstmt.setString(2, o.getIs_visto());
			pstmt.setInt(3, o.getId());
			int rsColum = pstmt.executeUpdate();		
			if (rsColum > 0) {
				pstmt = DBConn.getPstmt(conn, sql2);
				pstmt.setInt(1, o.getId());
				int rsColum2 = pstmt.executeUpdate();
				if (rsColum2 > 0) {
					result = true;
					return result;
				}
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return result;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}		
		return result;
	}
}
